import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children:[
      {path:"/",redirect:"/users"},
      {path:"/users",component: () => import('../components/User.vue')},
      {path:"/roles",component: () => import('../components/Role.vue')},
      {path:"/rights",component: () => import('../components/Limits.vue')}
    ]
  },
  {
    path: '/Login',
    name: 'Login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import( '../views/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 理由守卫
router.beforeEach((to,from,next)=>{
  var token = sessionStorage.getItem("token");
  if (!token) {
      if (to.path !="/login") {
        next("/login")
      }else{
        next();
      }
  }else{
    next()
  }
})

export default router
